﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 语言翻译
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-05
    /// </summary>
    internal class LanguageMapService : DataFactory, LanguageMapIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="languageTypeId">分类主键</param>
        /// <param name="language">语言种类</param>
        /// <returns></returns>
        public async Task<PageResult> GetList(PageInput input, string languageTypeId, string[] language)
        {
            try
            {
                using (var db = this.Basic)
                {
                    var parameters = new List<DbParameter>();
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("SELECT m.F_CreatorTime,m.F_EnCode,m.F_SignKey, m.F_FullName AS '" + language[0] + "' ");
                    for (var i = 1; i < language.Length; i++)
                    {
                        strSql.Append(", m" + i + ".F_FullName AS '" + language[i] + "' ");
                    }
                    strSql.Append("FROM Base_LanguageMap m ");
                    for (var j = 1; j < language.Length; j++)
                    {
                        strSql.Append("LEFT JOIN Base_LanguageMap m" + j + " ON m" + j + ".F_EnCode = m.F_EnCode AND m" + j + ".F_Language = '" + language[j] + "' ");
                    }
                    strSql.Append("WHERE m.F_Language = '" + language[0] + "' AND m.F_LanguageTypeId = @languageTypeId ");
                    parameters.Add(DataHelper.CreateParameter("@languageTypeId", languageTypeId));
                    if (!string.IsNullOrEmpty(input.keyword))
                    {
                        strSql.Append(" AND (m.F_FullName like @F_FullName ");
                        for (var i = 1; i < language.Length; i++)
                        {
                            strSql.Append("OR m" + i + ".F_FullName like @F_FullName ");
                        }
                        strSql.Append(")");
                        parameters.Add(DataHelper.CreateParameter("@F_FullName", "%" + input.keyword + "%"));
                    }
                    input.sidx = "F_CreatorTime";
                    input.sord = "asc";
                    var sql = strSql.ToString();
                    return await PageHelper.GetPageResultAsync(db, input, strSql.ToString(), parameters.ToArray());
                }
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <returns></returns>
        public async Task<List<LanguageMapEntity>> GetList(string fnCode)
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<LanguageMapEntity>().Where(m => m.EnCode == fnCode).OrderBy(o => o.SortCode).ToListAsync();
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="whereLambda">条件</param>
        /// <returns></returns>
        public async Task<bool> IsExist(Expression<Func<LanguageMapEntity, bool>> whereLambda)
        {
            using (var db = this.Basic)
            {
                return (await db.GetCountAsync(whereLambda) > 0);
            }
        }
        /// <summary>
        /// 重复验证（翻译标记）
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <param name="signKey">翻译标记</param>
        /// <returns></returns>
        public async Task<bool> IsExistBySignKey(string fnCode, string signKey)
        {
            using (var db = this.Basic)
            {
                if (!string.IsNullOrEmpty(fnCode))
                {
                    var currentCount = await db.GetCountAsync<LanguageMapEntity>(m => m.EnCode == fnCode);
                    var count = await db.GetCountAsync<LanguageMapEntity>(m => m.SignKey == signKey && m.EnCode != fnCode);
                    return (count >= currentCount);
                }
                else
                {
                    return (await db.GetCountAsync<LanguageMapEntity>(m => m.SignKey == signKey) > 0);
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <returns></returns>
        public async Task<int> Delete(string fnCode)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync<LanguageMapEntity>(m => m.EnCode == fnCode);
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="fnCode">编码</param>
        /// <param name="entitys">实体对象</param>
        public async Task Create(string fnCode, List<LanguageMapEntity> entitys)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.DeleteAsync<LanguageMapEntity>(m => m.EnCode == fnCode);
                await db.InsertAsync(entitys);
                await db.CommitTransactionAsync();
            }
        }

    }
}